package homework;

import queuecode.Queue;

import java.text.DecimalFormat;
import java.util.concurrent.ExecutorService;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/15 5:09
 */
public class Deque<E> {

    private E[] data;
    private int front,tail;
    private int size;

    public Deque(int capacity){
        data = (E[]) new Object[capacity];
        front = 0;
        tail = 0;
        size = 0;
    }

    public Deque(){
        this(10);
    }

    public int getCapacity(){
        return data.length;
    }



    public int getSize() {
        return size;
    }


    public boolean isEmpty() {
        return size==0;
    }

    public void addLast(E e){
        if(size == getCapacity()){
            resize(getCapacity()*2);
        }

        data[tail] = e;
        tail=(tail+1) % data.length;
        size++;

    }

    private void resize(int newCapacity) {
        E[]  newData = (E[])new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newData[i] = data[(i+front) % data.length];
        }
        data = newData;
        front = 0;
        tail = size;
    }


    public void addFront(E e){
        if(size == getCapacity()){
            resize(getCapacity()*2);
        }
        front = front==0? data.length-1:front-1;
        data[front] = e;
        size++;
    }

    public E removeFront(){
        if(isEmpty()){
            throw new IllegalArgumentException("队列为null");
        }
        E res = data[front];
        data[front] = null;
        front=(front+1)%data.length;
        size--;
        if(getSize() == getCapacity()/4 && getCapacity()/2 !=0){
            resize(getCapacity() / 2);
        }

        return  res;
    }




    public E removeLast(){
        if(isEmpty()){
            throw new IllegalArgumentException("队列为null");
        }

        tail=tail==0? data.length-1:tail-1;
        E res =  data[tail];
        size--;
        if(getSize() == getCapacity()/4 && getCapacity()/2 !=0){
            resize(getCapacity() / 2);
        }
        return  res;

    }

        public E  getFront(){
            if(isEmpty()){
                throw new IllegalArgumentException("队列为null");
            }

            return data[front];
        }


        public E getLast(){
            if(isEmpty()){
                throw new IllegalArgumentException("队列为null");
            }
            int index = tail==0? data.length-1:tail -1;
            return data[index];
        }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Deque: size = %d, capacity = %d\n",size,getCapacity()));
        res.append("front [");
        for (int i = 0; i < size; i++) {
            res.append(data[(i+front)% data.length] );
            if ( i !=size-1){
                res.append(", ");
            }
        }
        res.append("] tail");
        return res.toString();
    }





    public static void main(String[] args) {
        Deque<Integer> dq = new Deque<>();
        for (int i = 0; i < 16; i++) {
            if(i%2==0)dq.addLast(i);
            else dq.addFront(i);
        }
        System.out.println(dq);


        System.out.println();
        for (int i = 0; i < 16; i++) {
            if(i % 2 ==0)dq.removeFront();
            else dq.removeLast();
        }
        System.out.println(dq);
    }
}
